<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-button type="primary" @click="partreturn()">返回</el-button>
        <el-button type="primary" @click="addOrUpdateHandle()">新增</el-button>
        <!-- <el-button
          type="danger"
          @click="deleteHandle()"
          :disabled="dataListSelections.length <= 0"
        >批量删除</el-button> -->
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;"
    >
      <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
      <el-table-column prop="id" header-align="center" align="center" label="心愿单编号"></el-table-column>
      <!-- <el-table-column prop="partnerId" header-align="center" align="center" label="伴读对象id"></el-table-column> -->
      <el-table-column prop="title" header-align="center" align="center" label="心愿名称"></el-table-column>
      <el-table-column prop="wishInfo" header-align="center" align="center" label="心愿缩略图">
        <template slot-scope="scope">
          <img :src="scope.row.wishInfo" style="width:40px;height:40px;">
        </template>
      </el-table-column>
      <el-table-column prop="detail" header-align="center" align="center" label="心愿描述"></el-table-column>
      <el-table-column prop="targetCredit" header-align="center" align="center" label="所需积分"></el-table-column>
      <el-table-column prop="donateCredit" header-align="center" align="center" label="已捐积分">
        <template slot-scope="scope">
          <span v-if="scope.row.donateCredit == null">--</span>
          <span v-else>
              {{scope.row.donateCredit}}
          </span>
        </template>
      </el-table-column>
      <el-table-column prop="wishStatus" header-align="center" align="center" label="心愿状态">
        <template slot-scope="scope">
          {{scope.row.wishStatus==1?'进行中':''}}
          {{scope.row.wishStatus==2?'帮助中':''}}
          {{scope.row.wishStatus==3?'已完成':''}}
          <!-- {{scope.row.wishStatus==4?'暂停状态':''}} -->
          {{scope.row.wishStatus==0?'新建状态':''}}
        </template>
      </el-table-column>

      <el-table-column prop="whyUrl" header-align="center" align="center" label="申请理由 ">
        <template slot-scope="scope">
          <img :src="scope.row.whyUrl" style="width:40px;height:40px;">
        </template>
      </el-table-column>
      <!-- <el-table-column prop="isAlive" header-align="center" align="center" label="状态">
        <template slot-scope="scope">
          <div>{{scope.row.isAlive== 1?'显示':''}}</div>
          <div class="active">{{ scope.row.isAlive === 0 ? '不显示': '' }}</div>
        </template>
      </el-table-column> -->
      <el-table-column prop="teacherName" header-align="center" align="center" label="伴学老师 "></el-table-column>
      <el-table-column prop="teacherId" header-align="center" align="center" label="老师编号"></el-table-column>
      <el-table-column prop="teacherWhy" header-align="center" align="center" label="推荐理由 " :show-overflow-tooltip="true"></el-table-column>
      <el-table-column prop="gmtUpdate" header-align="center" align="center" label="创建时间"></el-table-column>
      <el-table-column prop="gmtCreated" header-align="center" align="center" label="最后更新时间"></el-table-column>
      <el-table-column fixed="right" header-align="center" align="center" width="150" label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="wishsee(scope.row.id)">查看</el-button>
          <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
          <!-- <el-button
            type="text"
            size="small"
            @click="status(scope.row.id)"
          >{{scope.row.isAlive=='1'?'不显示':'显示'}}</el-button> -->

          <!-- <el-button type="text" size="small" @click="deleteHandle(scope.row.id)">删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
    ></el-pagination>
    <!-- 查看 -->
    <wish-see v-if="partwishsee" ref="seewish" @refreshDataList="init"></wish-see>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="init"></add-or-update>
  </div>
</template>
<style >
.active {
  color: #f00;
}
</style>
<script>
import AddOrUpdate from "./partnerwish-add-or-update";
import WishSee from "./wish-see";
export default {
  data() {
    return {
      dataForm: {
        key: "",
        id: "",
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false,
      partwishsee:false,
    };
  },
  components: {
    AddOrUpdate,
    WishSee
  },
  props:['id'],
  watch:{
    partnerId(){
      return this.id
    }
  },
  mounted () {
    this.init()
  },
  methods: {
    // 获取数据列表
    init() {
      this.dataListLoading = true;
      this.$http({
        url: this.$http.adornUrl("/partner/partnerwish/list"),
        method: "get",
        params: this.$http.adornParams({
          page: this.pageIndex,
          limit: this.pageSize,
          key: this.dataForm.key,
          partnerId:this.id,
        })
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.dataList = data.page.list;
          this.totalPage = data.page.totalCount;
        } else {
          this.dataList = [];
          this.totalPage = 0;
        }
        this.dataListLoading = false;
      });
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.init();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.init();
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val;
    },
    // 状态 OK
     status(id) {
      this.dataForm.id = id;
      if (this.dataForm.id) {
        this.$http({
          url: this.$http.adornUrl(
            `/partner/partnerwish/disable/${this.dataForm.id}`
          ),
          method: "get",
          params: this.$http.adornParams()
        }).then(({ data }) => {
          this.init();
        });
      }
    },
    // 新增 / 修改ok
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true;
      console.log(id)  
        let obj = {};
        if(id){
          obj.id = id;
        }
      obj.partnerId = this.id;
      console.log(obj.partnerId)
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(obj);
      });
    },
    // 查看
    wishsee(id){
        this.partwishsee= true;
        this.$nextTick(() => {
            this.$refs.seewish.init(id);
          });
      },
    // 删除ok
    deleteHandle(id) {
      var ids = id
        ? [id]
        : this.dataListSelections.map(item => {
            return item.id;
          });
      this.$confirm(
        `确定对[id=${ids.join(",")}]进行[${id ? "删除" : "批量删除"}]操作?`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      ).then(() => {
        this.$http({
          url: this.$http.adornUrl("/partner/partnerwish/delete"),
          method: "post",
          data: this.$http.adornData(ids, false)
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.$message({
              message: "操作成功",
              type: "success",
              duration: 1500,
              onClose: () => {
                this.init(this.dataForm.id);
              }
            });
          } else {
            this.$message.error(data.msg);
          }
        });
      });
    },
    partreturn() {
      this.$emit('goback');
    }
  }
};
</script>
